package com.yyk.yykywd.mvvm.view_model

import android.text.Editable
import android.text.TextWatcher
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.yyk.yykywd.bean.LoginLoginInfo
import com.yyk.yykywd.mvvm.entity.MeetingBean
import com.yyk.yykywd.mvvm.entity.SearchCustomerBean
import com.yyk.yykywd.mvvm.model.CustomerModel
import com.yyk.yykywd.mvvm.model.MeetingModel
import kotlinx.coroutines.launch
import java.util.ArrayList

/**
 * 文件描述：
 * Created by guo on 2020-01-08.
 */
class SearchCustomerViewModel : BaseViewModel() {
    var type: String = ""
    private val customerModel by lazy {
        CustomerModel()
    }

    var mList: MutableList<SearchCustomerBean.Data> = ArrayList()

    var searchText = ""

    private var _searchInfo = MutableLiveData<SearchCustomerBean>()
    val searchInfo: LiveData<SearchCustomerBean>
        get() = _searchInfo

    var searchWatcher: TextWatcher = object : TextWatcher {
        override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
        override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
            searchText = s.toString()
            refresh(mList)
            searchInfo()
        }

        override fun afterTextChanged(s: Editable) {}
    }

    fun searchInfo() {
        viewModelScope.launch {
            customerModel.searchCustomer(type,searchText, object : CustomerModel.GetCustomerInfo {
                override fun getInfo(info: SearchCustomerBean) {
                    info.data?.let { mList.addAll(it) }
                    if (mList.isNotEmpty()) {
                        _status.value = BusinessApiStatus.DONE
                    }
                    _searchInfo.value = info
                }

                override fun onError(e: String) {
                    _status.value = BusinessApiStatus.ERROR
                }

            })
        }
    }
}